package structure;

import java.util.ArrayList;

public class state {

	private String label;
	private ArrayList<item> items;
	private boolean isFinal;
	private boolean isStart;
	
	public state(){
		
		this.label= null;
		this.items= null;
		this.isFinal= false;
		this.isStart= false;
	}
	
	public state(String label, ArrayList<item> items, boolean isFinal, boolean isStart){

		this.label= label;
		this.items= items;
		this.isFinal= isFinal;
		this.isStart= isStart;
	}
	
	//get
	public String getLabel(){
		return this.label;
	}
	
	public ArrayList<item> getItems(){
		return this.items;
	}
	
	public boolean isFinal(){
		return this.isFinal;
	}
	
	public boolean isStart(){
		return this.isStart;
	}
	//potenzialmente inutile
	public ArrayList<String> getIncomingArc(){
		
		ArrayList<String> toReturn= new ArrayList<String>();
		for(item i : this.items){
			
			String right= i.getRightside();
			for(int j=0; j<right.length(); j++){
				
				String tmpS= right.substring(j, j+1);
				if(tmpS.equals(".") && right.indexOf(".") > 0 ){
					toReturn.add(right.substring(j-1, j));
				}
			}
		}
		return toReturn;
	}
	
	public ArrayList<String> getOutgoingArc(){

		ArrayList<String> toReturn= new ArrayList<String>();
		/*for(item i : this.items){
			
			String right= i.getRightside();
			for(int j=0; j<right.length()-1; j++){
				
				String tmpS= right.substring(j, j+1);
				if(tmpS.equals(".") ){
					toReturn.add(right.substring(j+1, j+2));
				}
			}
		}*/
		
		for(item i : this.items){
			
			toReturn.add(i.getAfterDot());
		}
		return toReturn;
	}
	//
	
	//set
	
	//
	
	public boolean containItem(item i){
		
		for(item ii : this.items){
			
			if(ii.sameAs(i))
				return true;
		}
		return false;
	}
	
	public boolean sameItem(ArrayList<item> items){

		
		if(this.items.size() != items.size()) 
			return false;
		else{
			for(int i=0; i<this.items.size(); i++){
	
				if(!this.items.get(i).equals(items.get(i))){
					
					return false;
				}
			}
		}
		
		return true;
	}
	
	public void printItAll(){
		
		System.out.println("Label= " + this.label);
		for(item i : this.items){
			
			System.out.print("\t");
			i.printItAll();
		}
		System.out.println("Incoming arc= " + this.getIncomingArc());
		System.out.println("Outcoming arc= " + this.getOutgoingArc());
		
		if(this.isFinal)
			System.out.println("\tThis state is a final state");
		
		if(this.isStart)
			System.out.println("\tThis state is the start state");
		

		System.out.println("");
	}
}
